package com.genesis.yunnanji.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.SpecialtyAdapter;
import com.genesis.yunnanji.bean.SpecialtyBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;


public class Specialty extends BaseActivity {
    @ViewInject(R.id.tb_special_toolbar)
    private Toolbar mToolbar;
    @ViewInject(R.id.rv_special_rv)
    private RecyclerView mRecyclerView;
    @ViewInject(R.id.bga_special_refresh)
    private TwinklingRefreshLayout refreshLayout;
    @ViewInject(R.id.tv_special_title)
    private TextView mTitle;
    private int page = 1;
    private List<SpecialtyBean.ResultBean.ListBean> list;
    private SpecialtyAdapter adapter;
    private String sTitle;
    private String sId;
    private String dizhou = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_special);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        sTitle = getIntent().getStringExtra("title");
        sId = getIntent().getStringExtra("id");
        dizhou = getIntent().getStringExtra("dizhou");
        list = new ArrayList<>();
        StatusBarUtil.setColor(this, getResources().getColor(R.color.main_color), 2);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mTitle.setText(sTitle);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(context).size((int) getResources().getDimension(R.dimen.x20)).color(Color.parseColor("#f5f5f5")).build());

        getData(null);
    }

    @Override
    protected void setUpView() {
        LoadingView loadingView = new LoadingView(context);
        refreshLayout.setBottomView(loadingView);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                page = 1;
                list.removeAll(list);
                getData(refreshLayout);

            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                page += 1;
                getData(refreshLayout);

            }
        });
    }


    private void getData(final TwinklingRefreshLayout refreshLayout) {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.SPCIAL_LIST);
        params.addBodyParameter("categoryid", sId);
        params.addBodyParameter("p", page + "");
        params.addBodyParameter("dizhouid", dizhou);
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();

                Log.e("feiyi", jsonObject.toString());

                SpecialtyBean specialtyBean = gson.fromJson(jsonObject.toString(), SpecialtyBean.class);
                for (int j = 0; j < specialtyBean.getResult().getList().size(); j++) {
                    list.add(specialtyBean.getResult().getList().get(j));
                }
                if (list.size() > 0) {
                    nodata.setVisibility(View.GONE);
                } else {
                    nodata.setVisibility(View.VISIBLE);
                }
                if (page == 1) {
                    mRecyclerView.setLayoutManager(new GridLayoutManager(context, 2));
                    mRecyclerView.setNestedScrollingEnabled(false);
                    adapter = new SpecialtyAdapter(context, list);
//                    adapter.setEmptyView(R.layout.layout_empty);
                    mRecyclerView.setAdapter(adapter);
                    LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.x15));
                    LinearLayout lineLayout = new LinearLayout(Specialty.this);
                    lineLayout.setOrientation(LinearLayout.VERTICAL);
                    lineLayout.setLayoutParams(params);
                    lineLayout.setBackgroundColor(getResources().getColor(R.color.divider));
                    adapter.addHeaderView(lineLayout);
                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            Intent intent = new Intent(context, WebActivity.class);
                            intent.putExtra("url", list.get(position).getTaget_link() + "?__apptoken__=" + genesisUtils.getAppToken() + "&from=app");
                            startActivity(intent);
                        }
                    });

                } else {
                    adapter.notifyDataSetChanged();
                }

            }


            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
                if (refreshLayout != null) {
                    refreshLayout.finishRefreshing();
                    refreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

}
